package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.adapter.DropDownPoupWindowAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.TableContentEngineeringInstallForManagerAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.TableLeftEngineeringInstallForManagerAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallStatuBean;
import com.cloudsigns.factorymanager2.engineering.customview.SpinerPopWindow;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallStatusFormanagerPresenter;
import com.cloudsigns.factorymanager2.project.activity.EngineeringInstallationScheduleTableActivity;
import com.cloudsigns.factorymanager2.project.activity.ProductStatusTableActivity;
import com.cloudsigns.factorymanager2.project.activity.ProductionScheduleActivity;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.customview.NoscrollListView;
import com.cloudsigns.factorymanager2.project.customview.SyncHorizontalScrollView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EngineeringManagerStatusTableActivity extends Activity implements View.OnClickListener{

    private View ll_op_one, ll_op_two;
    private ImageView img_op_one, img_op_two;
    private ImageView img_tri_one, img_tri_two;
    private RelativeLayout rl_back, rl_head;
    private TextView tv_title;
    private TextView tv_head_title;

    /*表格*/
    public static final int MODULE_TABLE = 1;
    /*状态*/
    public static final int MODULE_STATU = 2;
    /*项目选择*/
    public static final int MODULE_SELECT = 3;


    /*表格*/
    public static final int ORDER_INSTALL = 1;
    /*状态*/
    public static final int ORDER_CONSTRUCT = 2;
    /*项目选择*/
    public static final int ORDER_BATCH = 3;
    /*表格*/
    public static final int ORDER_PUBLICATION = 4;
    /*状态*/
    public static final int ORDER_MAINTAIN = 5;
    /*项目选择*/
    public static final int ORDER_SURCEY = 6;

    private int currentOrderType = ORDER_INSTALL;


    private SyncHorizontalScrollView mHeaderHorizontal;
    private SyncHorizontalScrollView mDataHorizontal;

    /*表头 日期*/
    private LinearLayout ll_year_title;
    private NoscrollListView lvLeft;
    private NoscrollListView lvData;
    private TableLeftEngineeringInstallForManagerAdapter mLeftAdapter;
    private TableContentEngineeringInstallForManagerAdapter mDataAdapter;
    private LayoutInflater layoutInflater;
    private String TAG = EngineeringManagerStatusTableActivity.class.getSimpleName();
    //private EngineeringOrderListForMapPresenter engineeringOrderListForMapPresenter;
    private EngineeringInstallStatusFormanagerPresenter engineeringInstallStatusFormanagerPresenter;
    private List<EngineeringInstallStatuBean> engineeringInstallStatuBeanList = new ArrayList<>();
    private HashMap<String, List<EngineeringInstallStatuBean>> feedBackStatuMap;
    private HashMap<String, List<EngineeringInstallStatuBean>> orderStatukMap;
    List<EngineeringInstallStatuBean> tableLeftDataList = new ArrayList<>();
    List<String> tableHeadDataList = new ArrayList<>();

    private String selectedProjectName = "";

    private SpinerPopWindow<String> popwindow;
    private DropDownPoupWindowAdapter popwindowAdapter;
    private List<String> popStrLs = new ArrayList<String>();

    //EngineeringInstallWorkOrderInfoForSuperFragment 工单详情  EngineeringInstallDailyFeedBackDetailActivity  反馈详情

    private DropDownPoupWindow popupWindow;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineeringmanager_status_table);


        init();


    }


    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_head = findViewById(R.id.rl_head);
        ll_op_one = findViewById(R.id.ll_op_one);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_two = findViewById(R.id.ll_op_two);

        img_op_one = findViewById(R.id.img_op_one);
        img_op_two = findViewById(R.id.img_op_two);

        img_tri_one = findViewById(R.id.img_tri_one);
        img_tri_two = findViewById(R.id.img_tri_two);

        tv_title.setText("工程管理");

        img_op_one.setBackgroundResource(R.mipmap.title_icon_menu);
        img_op_two.setBackgroundResource(R.mipmap.icon_hand);



        ll_op_one.setVisibility(View.GONE);
        ll_op_two.setVisibility(View.VISIBLE);

        rl_back.setVisibility(View.VISIBLE);
        rl_head.setVisibility(View.GONE);

        ll_op_one.setOnClickListener(this);
        ll_op_two.setOnClickListener(this);

        rl_back.setOnClickListener(this);

        lvLeft = (NoscrollListView) findViewById(R.id.lv_left);
        lvData = (NoscrollListView) findViewById(R.id.lv_data);
        tv_head_title = findViewById(R.id.tv_head_title);


        mDataHorizontal = (SyncHorizontalScrollView) findViewById(R.id.data_horizontal);
        mHeaderHorizontal = (SyncHorizontalScrollView) findViewById(R.id.header_horizontal);

        ll_year_title = findViewById(R.id.ll_year_title);

        /*实现两个scrollview联动*/
        mDataHorizontal.setScrollView(mHeaderHorizontal);
        mHeaderHorizontal.setScrollView(mDataHorizontal);

        tv_head_title.setText("工程列表");

        mLeftAdapter = new TableLeftEngineeringInstallForManagerAdapter(this, TAG);
        mLeftAdapter.setData(tableLeftDataList);
        lvLeft.setAdapter(mLeftAdapter);



        mDataAdapter = new TableContentEngineeringInstallForManagerAdapter(this, TAG);
        mDataAdapter.setData(engineeringInstallStatuBeanList);


        lvData.setAdapter(mDataAdapter);

//        popwindow = new SpinerPopWindow<String>(this, this);
//        popwindowAdapter = new DropDownPoupWindowAdapter(this, TAG);
//        popStrLs.add("显示全部");
//        popwindowAdapter.setData(popStrLs);
//        popwindow.setAdapter(popwindowAdapter);

        layoutInflater = getLayoutInflater();
    }

    /**
     * 功能2
     *
     * @param anchor
     */
    private void showEngineeringPopupWindwo(View anchor) {
        if (null != popupWindow)
            popupWindow.dismiss();
        img_tri_two.setVisibility(View.VISIBLE);
        popupWindow = new DropDownPoupWindow(this, TAG, MODULE_TABLE, R.layout.popwindow_map_for_manager_engineering, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // backgroundAlpha(0.4f);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                img_tri_two.setVisibility(View.GONE);
            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }


    /**
     * 设置添加屏幕的背景透明�?
     *
     * @param bgAlpha
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.ll_op_one:
//                 showModulePopupWindow(view);
                break;
            case R.id.ll_op_two:
                showEngineeringPopupWindwo(view);
                break;

            case R.id.rl_back:
                finish();
                break;
            // 工程安装
            case R.id.ll_engineering_installation:
//                mIntent = new Intent(EngineeringInstallStatusTableForManagerActivity1.this, EngineeringInstallStatusTableForManagerActivity.class);
                mIntent = new Intent(EngineeringManagerStatusTableActivity.this, EngineeringInstallStatusTableForManagerActivity1.class);
                startActivity(mIntent);
                break;
            // 工程施工
            case R.id.ll_engineering_construct:
                mIntent = new Intent(EngineeringManagerStatusTableActivity.this, ProductionScheduleActivity.class);
                startActivity(mIntent);
                break;
            // 批量安装
            case R.id.ll_batch_engineering:
                mIntent = new Intent(EngineeringManagerStatusTableActivity.this, EngineeringInstallationScheduleTableActivity.class);
                startActivity(mIntent);
                break;
            // 上刊
            case R.id.ll_publication:
                mIntent = new Intent(EngineeringManagerStatusTableActivity.this, PublicationStatusTableForManagerActivity2.class);
                startActivity(mIntent);
                break;
            // 维修
            case R.id.ll_maintenance:
                mIntent = new Intent(EngineeringManagerStatusTableActivity.this, ProductStatusTableActivity.class);
                startActivity(mIntent);
                break;
            // 勘查
            case R.id.ll_survey:
                mIntent = new Intent(EngineeringManagerStatusTableActivity.this, ProductStatusTableActivity.class);
                startActivity(mIntent);
                break;
            // 勘察
            case R.id.ll_patrol:
                mIntent = new Intent(EngineeringManagerStatusTableActivity.this, PatrolStatusTableForManagerActivity.class);
                startActivity(mIntent);
                break;
        }
    }
}
